package com.taobao;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.app.taobao.RequestTaoBao;
import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.response.ItemcatsGetResponse;
import com.taobao.api.response.ItempropsGetResponse;
import com.utils.UtilsFile;

public class Itemcats {
	
	public static void main(String[] args) throws ApiException, InterruptedException {
		String path = "c:/taobao2";
		RequestTaoBao req = new RequestTaoBao();
		ItemcatsGetResponse itemcatsGetRequest = req.itemcatsGetRequest(0L);	
		File file = new File(path);
		if(!file.exists()){
			file.mkdir();
		}
		file = new File(file.getPath() + "/itemcats_root.xml");
		
		UtilsFile.LogToFile2(file, itemcatsGetRequest.getBody(), false);
		List<ItemCat> itemCats2 = itemcatsGetRequest.getItemCats();
		for (ItemCat itemCat : itemCats2) {
			itemcatsGetRequest = req.itemcatsGetRequest(itemCat.getCid());
			file = new File(path + "/" +itemCat.getCid());
			if(!file.exists()){
				file.mkdir();
			}
			file = new File(file.getPath() + "/" + itemCat.getCid() + ".xml");
			UtilsFile.LogToFile2(file, itemcatsGetRequest.getBody(), false);
			
			String path1 = file.getParent();
			if(itemCat.getIsParent()){
				itemCats2 = itemcatsGetRequest.getItemCats();
				for (ItemCat itemCat3 : itemCats2) {
					if(itemCat3.getIsParent()){
						itemcatsGetRequest = req.itemcatsGetRequest(itemCat3.getCid());
						File file1 = new File(path1 + "/" +itemCat3.getCid());
						if(!file1.exists()){
							file1.mkdir();
						}
						file1 = new File(file1.getPath() + "/" + itemCat3.getCid() + ".xml");
						UtilsFile.LogToFile2(file1, itemcatsGetRequest.getBody(), false);
						itemCats2 = itemcatsGetRequest.getItemCats();
						for (ItemCat itemCat2 : itemCats2) {
							ItempropsGetResponse itempropsGetRequest = req.itempropsGetRequest(itemCat2.getCid());
							File file2 = new File(file1.getParent() + "/" +itemCat2.getCid()+ ".xml");
							UtilsFile.LogToFile2(file2, itempropsGetRequest.getBody(), false);
						}
					}else{
						ItempropsGetResponse itempropsGetRequest = req.itempropsGetRequest(itemCat3.getCid());
						File file1 = new File(path1 + "/" +itemCat3.getCid()+ ".xml");
						UtilsFile.LogToFile2(file1, itempropsGetRequest.getBody(), false);
					}
					
				}	
			}else{
//				System.out.println(file.getPath() + "--" + itemCat.getCid() );
				ItempropsGetResponse itempropsGetRequest = req.itempropsGetRequest(itemCat.getCid());
				File file1 = new File(path1 + "/" +itemCat.getCid()+ ".xml");
				UtilsFile.LogToFile2(file1, itempropsGetRequest.getBody(), false);
			}
		}
		
		
		System.out.println("over");
		
	}
}
